//判断是否是闰年
function isLeapYear(year) {
	return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

//判断当月天数
function getDays(year, month) {
	var day = 0;
	if(month == 2) {
		if(isLeapYear(year)) {
			day = 29;
		} else {
			day = 28;
		}
	} else if(month == 4 || month == 6 || month == 9 || month == 11) {
		day = 30;
	} else {
		day = 31;
	}
	return day;
}

//累加年
function addYear(year) {
	var sumday = 0;
	for(var i = 1900; i < year; i++) {
		if(isLeapYear(i)) {
			sumday += 366;
		} else {
			sumday += 365;
		}
	}
	return sumday;
}

//累加月
function addMonth(year, month) {
	var sumday = 0;
	for(var i = 1; i < month; i++) {
		sumday += getDays(year, i);
	}
	return sumday;
}

//判断当月1日星期几
function getWeekdays(year, month) {
	var weekday = (addYear(year) + addMonth(year, month) + 1) % 7;
	return weekday;
}

//判断当月最后一天星期几
function getlastWeekdays(year, month) {
	var weekday = getWeekdays(year, month);
	var day = getDays(year, month);
	var lastweekday = (day + weekday - 1) % 7;
	return lastweekday;
}

//全部变更为两位数
function getNumbers(number) {
	if(number < 10) {
		return number = "0" + number;
	} else {
		return number;
	}
}

//计算现在日期
function getDates() {
	var time = new Date();
	var year = time.getFullYear();
	var month = getNumbers(time.getMonth() + 1);
	var day = getNumbers(time.getDate());
	var date = year + "年" + month + "月" + day + "日";
	return date;
}

//计算显示年月
function getYearMonth(year, month) {
	var month = getNumbers(month);
	yearMonth = year + "年" + month + "月";
	return yearMonth;
}

//输出日历
function outputCalendar(year, month) {
	var calendar = "<table border='1px' cellpadding='10px' cellspacing='0px'>";
	calendar += "<tr><td colspan='3'>现在日期： " + getDates() + "</td>";
	calendar += "<td colspan='4'>选择日期：" + getYearMonth(year, month) + "</td></tr>";
	calendar += "<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td >五</td><td >六</td></tr><tr>";

	for(i = 1; i <= getWeekdays(year, month); i++) {
		calendar += "<td></td>";
	}

	for(i = 1; i <= getDays(year, month); i++) {
		if(i != 1 && (getWeekdays(year, month) + i - 1) % 7 == 0) {
			calendar += "<tr>";
		}
		calendar += "<td>" + i + "</td>";
		if((getWeekdays(year, month) + i - 1) % 7 == 6) {
			calendar += "</tr>";
		}
	}

	for(i = 6; i > getlastWeekdays(year, month); i--) {
		calendar += "<td></td>";
	}
	return calendar;
}